﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Net;
using Newtonsoft.Json.Linq;

namespace Bakındur
{
    class SozlukBaslatici_API : Baslatici<TranslationResult>
    {
        private WebClient client;

        public SozlukBaslatici_API()
        {
            client = new WebClient();
        }

        private bool isBusy;
        public override bool IsBusy
        {
            get
            {
                return isBusy;
            }
            set
            {
                isBusy = value;
            }
        }

        private TranslationResult translationResult;
        public override TranslationResult Result
        {
            get
            {
                return translationResult;
            }
        }

        public override string Command
        {
            get
            {
                return "http://api.seslisozluk.com/?key=d5fe2050e3ca05f014fb7a31b158899e&lang_from=en&query=";
                //return "http://m.seslisozluk.com/?word=";
            }
        }

        public override string Shortcut
        {
            get { return "s"; }
        }

        public override void Execute()
        {
            IsBusy = true;
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

            try
            {
                client.DownloadStringAsync(new Uri(string.Format("{0}{1}", Command, Arguments)), "Sozluk");
            }
            catch
            {
                throw;
            }
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                translationResult = JsonConvert.DeserializeObject<TranslationResult>(e.Result);
            }
            catch
            {
                throw;
            }
            IsBusy = false;
        }
    }

    //jsonp1251833254285({
    //      "lang_from":"en",
    //      "lang_to":"",
    //      "query":"white",
    //      "translations":{
    //              "0":{"lang_pair":"en-tr","translation":"beyaz"},
    //              "1":{"lang_pair":"en-tr","translation":"beyaz renk"},
    //              "2":{"lang_pair":"en-tr","translation":"ak"},
    //              "3":{"lang_pair":"en-en","translation":"destitute of color, as in the cheeks,..."},
    //              "4":{"lang_pair":"en-en","translation":"reflecting to the eye all the rays of ..."},
    //              "5":{"lang_pair":"en-en","translation":"having the color of purity; free from ..."},
    //              "7":{"lang_pair":"en-de","translation":"wei\u00df , wei\u00dfes"},
    //              "8":{"lang_pair":"en-de","translation":"machte wei\u00df"},
    //              "9":{"lang_pair":"en-de","translation":"wei\u00dfe"},
    //              "10":{"lang_pair":"en-el","translation":"(adj) \u03bb\u03b5\u03c5\u03ba\u03cc..."},
    //              "11":{"lang_pair":"en-el","translation":"\u03ac\u03c3\u03c0\u03c1\u03bf\u03c2"},
    //              "12":{"lang_pair":"en-el","translation":"\u03bb\u03b5\u03c5\u03ba\u03cc, ..."}
    //              }
    //      })

    public class TranslationResult
    {
        public string lang_from { get; set; }
        public string lang_to { get; set; }
        public string query { get; set; }
        public Translate[] translations { get; set; }
    }

    public class Translate
    {
        public string lang_pair { get; set; }
        public string translation { get; set; }
    }
}
